//
//  YZDieOutTrackVC.m
//  VET
//
//  Created by 刘文靖 on 2018/7/21.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZDieOutTrackVC.h"
#import "YZNoteInputView.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>
#import "TZImagePickerController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "UIViewController+YZImagePicker.h"
#import "YZLineView.h"
#import "NSDate+YZDatePickView.h"
#import "YZActionSheet.h"
#import "YZNetworkManager.h"
#import "YZDieOutModel.h"
#import "YZRecordManager.h"
#import "YZAudioRecordVC.h"
#import "YZAutoCache.h"
#import "YZAutoItemModel.h"
#import "YZCameraVC.h"
#import "YZVideoVC.h"
@interface YZDieOutTrackVC ()<UIScrollViewDelegate,TZImagePickerControllerDelegate,YZNoteInputViewDelegate,YZActionSheetDelegate,YZMediaProtocol>
@property (strong, nonatomic) UIScrollView *mainScrollView;
@property (strong, nonatomic) YZNoteInputView *inputView;
@property (nonatomic, strong) NSMutableArray *selectedPhotos;
@property (nonatomic, strong) NSMutableArray *selectedAssets;
@property (nonatomic, strong) NSMutableDictionary *trackDic;
@property (nonatomic, assign) BOOL isSelectOriginalPhoto;
@property (nonatomic, strong) NSString *mediaPath;
@property (nonatomic, strong) UILabel *createTimeLabel;
@property (nonatomic, strong) UILabel *categoryLabel;
@property (nonatomic, strong) NSArray<YZAutoItemModel *> *items;
@property (nonatomic, assign) NSInteger audioMediaTime;
@property (nonatomic, strong) YZAutoItemModel *selectedAutoItemModel;
@property (nonatomic, strong) NSString *mediaOrientation;
@end

@implementation YZDieOutTrackVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _selectedPhotos = [NSMutableArray array];
    _selectedAssets = [NSMutableArray array];
    [self setupNavigationBar];
    [self setupMainView];
}

- (void)setupNavigationBar{
    self.title = @"死淘跟踪记录";
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 36, 25)];
    [rightButton setTitle:@"保存" forState:UIControlStateNormal];
    [rightButton setTitleColor:kColorWithHex(0x32BE60) forState:UIControlStateNormal];
    [rightButton setTitleColor:kColorWithHex(0xcecece) forState:UIControlStateDisabled];
    rightButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:18];
    [rightButton addTarget:self action:@selector(saveTrack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *righBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = righBarButtonItem;
}

- (void)setupMainView{
    self.mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    self.mainScrollView.backgroundColor  = kColorWithHex(0xffffff);
    self.mainScrollView.delegate = self;
    self.mainScrollView.alwaysBounceVertical = YES;
    self.mainScrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.mainScrollView];
    self.inputView = [[YZNoteInputView alloc] init];
    self.inputView.placeHolder = @"点击输入死淘分析、解剖记录、无害化处理…";
    self.inputView.delegate = self;
    [self.mainScrollView addSubview:self.inputView];
    [self.inputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.mas_equalTo(384);
    }];
    
    UIButton *createTimeBtn =[self createButtonWithTitle:@"记录时间"];
    [createTimeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.inputView.mas_bottom).offset(10);
        make.left.right.offset(0);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 54));
    }];
    self.createTimeLabel = [[UILabel alloc] init];
    self.createTimeLabel.textColor = kColorWithHex(0x434343);
    self.createTimeLabel.text = [NSDate yz_getDateString:[NSDate date] format:@"yyyy-MM-dd HH:mm"];
    self.createTimeLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
    [createTimeBtn addSubview:self.createTimeLabel];
    [self.createTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.right.offset(-47);
    }];
    
    
    [self.categoryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.right.offset(-47);
    }];
    
    UIButton *categoryBtn = [self createButtonWithTitle:@"笔记类别"];
    [categoryBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(createTimeBtn?createTimeBtn.mas_bottom:self.inputView.mas_bottom);
        make.left.right.offset(0);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 54));
        make.bottom.offset(-80);
    }];
    
    self.categoryLabel = [[UILabel alloc] init];
    self.categoryLabel.textColor = kColorWithHex(0x434343);
    self.categoryLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
    [categoryBtn addSubview:self.categoryLabel];
    [self.categoryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.right.offset(-47);
    }];
    
    [categoryBtn addTarget:self action:@selector(categoryBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}

- (UIButton *)createButtonWithTitle:(NSString *)title{
    UIButton *btn= [[UIButton alloc] init];
    btn.backgroundColor = [UIColor whiteColor];
    [self.mainScrollView addSubview:btn];
    
    UILabel *label = [[UILabel alloc] init];
    label.textColor = kColorWithHex(0x393939);;
    label.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];;
    label.text = title;
    [btn addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.height.mas_equalTo(14);
        make.left.offset(24);
    }];
    
    UIImageView *arrowIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ico_right"]];
    [btn addSubview:arrowIV];
    [arrowIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.right.offset(-29);
    }];
    
    YZLineView *lineView = [[YZLineView alloc] init];
    [btn addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(.5);
        make.left.offset(24);
        make.centerX.bottom.offset(0);
    }];
    return btn;
}

#pragma mark -clickEvents
- (void)saveTrack{
    if (!self.categoryLabel.text.length || (!self.inputView.textView.text.length && !self.mediaPath.length && !self.selectedPhotos.count)) {
        [SVProgressHUD yz_showMessage:@"请完善选项内容后再保存!"];
        return;
    }
    [SVProgressHUD show];
    self.trackDic = [NSMutableDictionary dictionary];
    self.trackDic[@"vetDieoutObj"] = @(self.trackId);
    self.trackDic[@"trackText"] = self.inputView.textView.text;
    self.trackDic[@"trackTime"] = @([NSDate yz_getDateTimeInterval:self.createTimeLabel.text format:@"yyyy-MM-dd HH:mm"]);
    if (self.selectedAutoItemModel) {
        self.trackDic[@"trackType"] = @(self.selectedAutoItemModel.itemKey);
    }
    if (self.selectedPhotos || self.mediaPath) {
        PHAsset *phAsset = self.selectedAssets.firstObject;
        if (self.mediaPath) {//视频/音频
            NSString *mediaType = @"audio/aac";
            if (phAsset.mediaType == PHAssetMediaTypeVideo) {
                mediaType = @"video/mp4";
            }
            @weakify(self)
            [YZNetworkManager uploadVideoWithApi:@"/api/dieout/track/attachement" params:nil videoPath:self.mediaPath name:@"attachments" withHeaders:nil success:^(NSObject *obj) {
                @strongify(self)
                NSArray *attachments = ((NSDictionary *)obj)[@"data"];
                NSMutableArray *attachmentList = [NSMutableArray array];
                [attachmentList addObject:@{@"attachmentUrl":attachments.firstObject,@"mediaType":mediaType,@"mediaTime":@(self.audioMediaTime),@"mediaOrientation":self.mediaOrientation}];
                self.trackDic[@"attachmentList"] = attachmentList;
                [self publishTrack];
            } fail:^(NSError *error) {
                [SVProgressHUD yz_showError:@"网络异常,请重试"];
                return ;
            }];
        }else{//图片
            @weakify(self)
            [YZNetworkManager uploadImgsWithApi:@"/api/dieout/track/attachement" params:nil imgs:self.selectedPhotos name:@"attachments" withHeaders:nil success:^(NSObject *obj) {
                @strongify(self)
                NSArray *attachments = ((NSDictionary *)obj)[@"data"];
                NSMutableArray *attachmentList = [NSMutableArray array];
                for (int i = 0 ; i < attachments.count; i++) {
                    [attachmentList addObject:@{@"attachmentUrl":attachments[i],@"mediaType":@"image/png"}];
                }
                self.trackDic[@"attachmentList"] = attachmentList;
                [self publishTrack];
            } fail:^(NSError *error) {
                [SVProgressHUD yz_showError:@"网络异常,请重试"];
                return ;
            }];
        }
    }else{
        [self publishTrack];
    }
}

- (void)publishTrack{
    [[YZDieOutModelAPI createDieoutTrack:self.trackDic] subscribeError:^(NSError * _Nullable error) {
        [SVProgressHUD yz_showErrorInfo:error];
    } completed:^{
        [SVProgressHUD yz_showMessage:@"发布成功"];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

- (void)categoryBtnClick{
    [[[YZAutoCache sharedInstance] getAutoCellItems:@"track_type"] subscribeNext:^(NSArray<YZAutoItemModel *> *items) {
        self.items = items;
        NSMutableArray *sheetTitles = [NSMutableArray array];
        for (YZAutoItemModel *item in items) {
            [sheetTitles addObject:item.itemValue];
        }
        YZActionSheet *sheet = [[YZActionSheet alloc] initWithTitle:nil sheetTitles:sheetTitles cancleBtnTitle:@"取消" sheetStyle:YZActionSheetDefault delegate:self];
        sheet.sheetHeight = 55;
        sheet.subtitleFont = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
        sheet.subtitleColor = kColorWithHex(0x393939);
        sheet.lineColor = kColorWithHex(0xD9D9D9);
        sheet.cancleHeight = 55;
        sheet.bottomMarginHeight = 15;
        sheet.cancelTitleFont = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
        sheet.cancelTitleColor = kColorWithHex(0x393939);
        [sheet show];
    }];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}

#pragma mark - YZActionSheetDelegate
/**
 点击actionSheet代理方法
 
 @param actionSheet actionSheet
 @param buttonIndex index
 */
- (void)actionSheet:(YZActionSheet *)actionSheet clickButtonAtIndex:(NSInteger)buttonIndex{
    self.selectedAutoItemModel = self.items[buttonIndex];
    self.categoryLabel.text = self.selectedAutoItemModel.itemValue;
    
}
- (void)actionSheetCancle:(YZActionSheet *)actionSheet{}

#pragma mark - YZNoteInputViewDelegate

- (void)audioBtnDidClick:(YZNoteInputView *)view{
    YZAudioRecordVC  *recordVC = [[YZAudioRecordVC alloc] init];
    recordVC.recordSuccessBlock = ^(NSString *recordPath, NSInteger timeInterval) {
        self.mediaPath = recordPath;
        self.audioMediaTime = timeInterval;
        [self.inputView showAudioViewWithAudioUrl:recordPath timeInterval:timeInterval];
    };
    [self presentViewController:recordVC animated:YES completion:nil];
}

- (void)imageBtnDidClick:(YZNoteInputView *)view{
    @weakify(self)
    [self yz_presentSelectSheetWithTitle:nil cancelButtonTitle:nil twoOtherButtonTitlesArray:@[@"从相册选择",@"拍照"] firstSelBTHandler:^{
        @strongify(self)
        [self pushTZImagePickerController:NO];
    } secondSelBTHandler:^{
        @strongify(self)
        YZCameraVC *cameraVC = [YZCameraVC new];
        cameraVC.delegate = self;
        [self presentViewController:cameraVC animated:YES completion:nil];
    }];
}

- (void)videoBtnDidClick:(YZNoteInputView *)view{
    YZVideoVC *videoVC = [YZVideoVC new];
    videoVC.delegate = self;
    [self presentViewController:videoVC animated:YES completion:nil];
}

- (void)audioDeleteDidClick:(YZNoteInputView *)view{
    [[NSFileManager defaultManager] removeItemAtPath:self.mediaPath error:nil];
    self.mediaPath = nil;
    
}

- (void)audioPlayDidClick:(YZNoteInputView *)view{
    [[YZRecordManager shareManager] startPlayRecorder:self.mediaPath];
}
#pragma mark -- YZMediaProtocol

- (void)getCurrentVideoAssert:(PHAsset *)assert andUrl:(NSURL *)url andCurrentOrientation:(AVCaptureVideoOrientation)orientation
{
    if (!assert) return;
    [SVProgressHUD show];
    switch (orientation) {
        case AVCaptureVideoOrientationPortrait:
            self.mediaOrientation = @"top";
            break;
        case AVCaptureVideoOrientationPortraitUpsideDown:
            self.mediaOrientation = @"bottom";
            break;
        case AVCaptureVideoOrientationLandscapeRight:
            self.mediaOrientation = @"right";
            break;
        case AVCaptureVideoOrientationLandscapeLeft:
            self.mediaOrientation = @"left";
            break;
        default:
            self.mediaOrientation = @"top";
            break;
    }
    TZImagePickerController *tzImagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
    tzImagePickerVc.sortAscendingByModificationDate = YES;
    [[TZImageManager manager] saveVideoWithUrl:url location:nil completion:^(NSError *error) {
        if (!error) {
            [[TZImageManager manager] getCameraRollAlbum:YES allowPickingImage:NO needFetchAssets:NO completion:^(TZAlbumModel *model) {
                [[TZImageManager manager] getAssetsFromFetchResult:model.result allowPickingVideo:YES allowPickingImage:NO completion:^(NSArray<TZAssetModel *> *models) {
                    TZAssetModel *assetModel = [models firstObject];
                    if (tzImagePickerVc.sortAscendingByModificationDate) {
                        assetModel = [models lastObject];
                    }
                    //视频保存到本地
                    [self getVideoOutputPathWithAsset:assetModel.asset];
                    [[TZImageManager manager] getPhotoWithAsset:assetModel.asset completion:^(UIImage *photo, NSDictionary *info, BOOL isDegraded) {
                        if (!isDegraded && photo) {
                            [self refreshCollectionViewWithAddedAsset:assetModel.asset image:photo];
                        }
                    }];
                }];
            }];
        } else {
           [SVProgressHUD yz_showError:@"视频获取失败"];
        }
    }];
}

- (void)getCurrentPhotoAssert:(PHAsset *)assert
{
    if (!assert) return;
    @weakify(self)
    [[TZImageManager manager] getOriginalPhotoWithAsset:assert completion:^(UIImage *photo, NSDictionary *info) {
        @strongify(self)
        if (photo) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self refreshCollectionViewWithAddedAsset:assert image:photo];
            });
        }
    }];
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- (void)preViewImage:(NSInteger)index{
    _selectedPhotos = self.inputView.notePics;
    id asset = _selectedPhotos[index];
    if (_selectedAssets.count > index) {
        asset = _selectedAssets[index];
    }
    BOOL isVideo = NO;
    if ([asset isKindOfClass:[PHAsset class]]) {
        PHAsset *phAsset = asset;
        isVideo = phAsset.mediaType == PHAssetMediaTypeVideo;
    }
    else if ([asset isKindOfClass:[ALAsset class]]) {
        ALAsset *alAsset = asset;
        isVideo = [[alAsset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo];
    }
    if (isVideo) { // perview video / 预览视频
        if (!self.mediaPath.length) return;
        TZVideoPlayerController *vc = [[TZVideoPlayerController alloc] init];
        vc.videoUrl = self.mediaPath;
        @weakify(self)
        vc.deleteButtonClickBlock = ^{
            @strongify(self)
            [[NSFileManager defaultManager] removeItemAtPath:self.mediaPath error:nil];
            self.mediaPath = nil;
            [self.selectedPhotos removeAllObjects];
            [self.selectedAssets removeAllObjects];
            self.inputView.selectedPhotos = self.selectedPhotos;
            self.inputView.selectedAssets = self.selectedAssets;
            [self updateInputViewHeightConstraint];
        };
        [self presentViewController:vc animated:YES completion:nil];
    } else{ // preview photos / 预览照片
        NSMutableArray * photoModels = [NSMutableArray arrayWithCapacity:_selectedPhotos.count];
        for (int i =0 ; i < _selectedPhotos.count; i++) {
            TZAssetModel *model = [[TZAssetModel alloc] init];
            model.cachedImage = _selectedPhotos[i];
            [photoModels addObject:model];
        }
        TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithSelectedAssets:_selectedAssets selectedPhotos:_selectedPhotos photoModels:photoModels index:index];
        imagePickerVc.maxImagesCount = 9;
        imagePickerVc.allowPickingGif = NO;
        imagePickerVc.allowPickingOriginalPhoto = NO;
        imagePickerVc.allowPickingMultipleVideo = NO;
        imagePickerVc.showSelectedIndex = YES;
        imagePickerVc.isSelectOriginalPhoto = _isSelectOriginalPhoto;
        @weakify(self)
        [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
            @strongify(self)
            self.selectedPhotos = [NSMutableArray arrayWithArray:photos];
            self.selectedAssets = [NSMutableArray arrayWithArray:assets];
            self.isSelectOriginalPhoto = isSelectOriginalPhoto;
            self.inputView.selectedPhotos = self.selectedPhotos;
            self.inputView.selectedAssets = self.selectedAssets;
            [self updateInputViewHeightConstraint];
            
        }];
        [self presentViewController:imagePickerVc animated:YES completion:nil];
    }
}


#pragma mark - TZImagePickerController
- (void)pushTZImagePickerController:(BOOL)allowPickingVideo {
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9- self.inputView.notePics.count columnNumber:3 delegate:self pushPhotoPickerVc:YES];
    // imagePickerVc.navigationBar.translucent = NO;
    
#pragma mark - 五类个性化设置，这些参数都可以不传，此时会走默认设置
    imagePickerVc.isSelectOriginalPhoto = _isSelectOriginalPhoto;
    
    // 1.设置目前已经选中的图片数组
    //imagePickerVc.selectedAssets = _selectedAssets; // 目前已经选中的图片数组
    imagePickerVc.allowTakePicture = NO; // 在内部显示拍照按钮
    imagePickerVc.allowTakeVideo = NO;   // 在内部显示拍视频按
    imagePickerVc.videoMaximumDuration = 10; // 视频最大拍摄时间
    [imagePickerVc setUiImagePickerControllerSettingBlock:^(UIImagePickerController *imagePickerController) {
        imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
    }];
    
    imagePickerVc.iconThemeColor = [UIColor colorWithRed:31 / 255.0 green:185 / 255.0 blue:34 / 255.0 alpha:1.0];
    imagePickerVc.showPhotoCannotSelectLayer = YES;
    imagePickerVc.cannotSelectLayerColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8];
    //    [imagePickerVc setPhotoPickerPageUIConfigBlock:^(UICollectionView *collectionView, UIView *bottomToolBar, UIButton *previewButton, UIButton *originalPhotoButton, UILabel *originalPhotoLabel, UIButton *doneButton, UIImageView *numberImageView, UILabel *numberLabel, UIView *divideLine) {
    //        [doneButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    //    }];
    // 3. 设置是否可以选择视频/图片/原图
    imagePickerVc.allowPickingVideo = allowPickingVideo;
    imagePickerVc.allowPickingImage = !allowPickingVideo;
    imagePickerVc.allowPickingOriginalPhoto = NO;
    imagePickerVc.allowPickingGif = NO;
    imagePickerVc.allowPickingMultipleVideo = NO; // 是否可以多选视频
    
    // 4. 照片排列按修改时间升序
    imagePickerVc.sortAscendingByModificationDate = YES;
    
    imagePickerVc.statusBarStyle = UIStatusBarStyleLightContent;
    
    // 设置是否显示图片序号
    imagePickerVc.showSelectedIndex = YES;
    
#pragma mark - 到这里为止
    
    // You can get the photos by block, the same as by delegate.
    // 你可以通过block或者代理，来得到用户选择的照片.
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        
    }];
    
    [self presentViewController:imagePickerVc animated:YES completion:nil];
}



#pragma mark - TZImagePickerControllerDelegate

/// User click cancel button
/// 用户点击了取消
- (void)tz_imagePickerControllerDidCancel:(TZImagePickerController *)picker {
    // NSLog(@"cancel");
}

// 这个照片选择器会自己dismiss，当选择器dismiss的时候，会执行下面的代理方法
// 如果isSelectOriginalPhoto为YES，表明用户选择了原图
// 你可以通过一个asset获得原图，通过这个方法：[[TZImageManager manager] getOriginalPhotoWithAsset:completion:]
// photos数组里的UIImage对象，默认是828像素宽，你可以通过设置photoWidth属性的值来改变它
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto infos:(NSArray<NSDictionary *> *)infos {
    [_selectedPhotos addObjectsFromArray:photos];
    [_selectedAssets addObjectsFromArray:assets];
    _isSelectOriginalPhoto = isSelectOriginalPhoto;
    _inputView.selectedPhotos = _selectedPhotos;
    _inputView.selectedAssets = _selectedAssets;
    [self updateInputViewHeightConstraint];
}

// If user picking a video, this callback will be called.
// If system version > iOS8,asset is kind of PHAsset class, else is ALAsset class.
// 如果用户选择了一个视频，下面的handle会被执行
// 如果系统版本大于iOS8，asset是PHAsset类的对象，否则是ALAsset类的对象
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingVideo:(UIImage *)coverImage sourceAssets:(id)asset {
    _selectedPhotos = [NSMutableArray arrayWithArray:@[coverImage]];
    _selectedAssets = [NSMutableArray arrayWithArray:@[asset]];
    _inputView.selectedPhotos = _selectedPhotos;
    _inputView.selectedAssets = _selectedAssets;
    [self updateInputViewHeightConstraint];
    // open this code to send video / 打开这段代码发送视频
    [self getVideoOutputPathWithAsset:asset];
   
    
}

- (void)getVideoOutputPathWithAsset:(id)asset{
    @weakify(self)
    [[TZImageManager manager] getVideoOutputPathWithAsset:asset presetName:AVAssetExportPreset640x480 success:^(NSString *outputPath) {
        @strongify(self)
        self.mediaPath = outputPath;
        [SVProgressHUD dismiss];
    } failure:^(NSString *errorMessage, NSError *error) {
        [SVProgressHUD yz_showError:@"视频获取失败"];
    }];
}

- (void)refreshCollectionViewWithAddedAsset:(id)asset image:(UIImage *)image{
    [_selectedAssets addObject:asset];
    [_selectedPhotos addObject:image];
    _inputView.selectedPhotos = _selectedPhotos;
    _inputView.selectedAssets = _selectedAssets;
    [self updateInputViewHeightConstraint];
    
    //    if ([asset isKindOfClass:[PHAsset class]]) {
    //        PHAsset *phAsset = asset;
    //        NSLog(@"location:%@",phAsset.location);
    //    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    if ([picker isKindOfClass:[UIImagePickerController class]]) {
        [picker dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)updateInputViewHeightConstraint{
    [self.inputView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(self.selectedPhotos.count?426:384);
    }];
}
#pragma clang diagnostic pop
@end
